#ifndef __VIDEO_FIEL_STREAM_H__
#define __VIDEO_FIEL_STREAM_H__

#include <map>
#include <mutex>
#include <string>
#include <functional>
#include "Pal/VideoDec.h"
// #include "Parser/Frame.h"
#include "json.h"
// #include "Config.h"
#include "mp4v2/mp4v2.h"
#include "Thread/Thread.h"
#include "StreamComposeMgr.h"
#include "Poller/Timer.h"
#include "AudioDecode.h"


namespace Media{

const std::string  videoPath =  "/mnt/sd/VideoFile/";
//const std::string  videoPath = "/nfsroot/StreamFile/VideoFile/";

class VideoDecode;

class VideoFileStream{
public:

	static VideoFileStream *instance(int channel);

	bool stop();

	bool start(StreamPlayInfo playInfo);

	bool timerTaskCb();
	
private:
	VideoFileStream(int channel);

	bool setFormat(VideoDecFormat *format);

	bool streamStart(std::string fileName);
	
	bool streamStop(void);

	void VideoThreadProc(Fdt::Thread *thread);

	void AudioThreadProc(Fdt::Thread *thread);

	bool parseMP4HeadInfo(MP4FileHandle inFile);

	bool sendVideoPacket(unsigned char *buffer, int buflen, uint64_t timestamp);

	bool doMP4ReadOneFrame(MP4FileHandle inFile, MP4TrackId tid, uint32_t sid, bool bIsVideo, uint32_t & outSendTime);

	bool sendAudioPacket(unsigned char *buffer, int buflen);

	void ThreadProc(Fdt::Thread *thread);

private:
	static std::map<int, VideoFileStream *> sm_streamMap;

	static std::recursive_mutex 			sm_mapMutex;
    
    VideoDecode *                           m_pVideoDecode;
	
	int 									m_videoChannel = -1;
	
	bool									m_started; //start作为启动标志,避免多个业务多次启停

	VideoDecPacket                          m_videoPacket;

	MP4FileHandle                           m_fileHd;

	std::mutex                              m_fileMutex;

	MP4TrackId                              m_videoTrackId;

	MP4TrackId                              m_audioTrackId;

	uint8_t *                               m_pSPSData;

	uint32_t *                              m_iSPSLen;

	uint8_t *                               m_pPPSData;

	uint32_t *                              m_iPPSLen;
	
	std::shared_ptr<Fdt::Timer>             m_taskTimer;

	TaskInfo                                m_taskInfo = {0};

	AudioDecode*                            m_pAudioDecode;

	AudioDecPacket                          m_audioPacket;

    StreamAbility                           m_playAbility;

    int 									m_audioChannel = -1;

    std::string                             m_fileStreamName;

    int                                     m_audioRateIndex;

    int                                     m_audioChannelNum;
	
	
	Fdt::Thread 						    m_videoThread{"VideoFileStream"};//thread放在最后,保证thread最后构造,最先析构
	Fdt::Thread 						    m_audioThread{"AudioFileStream"};//thread放在最后,保证thread最后构造,最先析构

};

}//namespace Media
#endif

